var core = require('core');
var sign = require('../sign');
var send = core.send;
var wechat = core.wechat;
var util = core.util;
var qr = util.qrcode;

var region = function(){
    core.api("open/getRegion", {}, function(err, doc){
        if(err)
            throw err;
        else{
            region = doc.docs;
        }
    });

}();

var GET = function(req, res, next){
    var query = req.query;
    var token = query.token;
    sign(token, function(doc){
        if(doc.code){
            doc.docs = {};
            req.error = doc;
            next();
        }else{
            core.api("merchant/list", {}, function(err, doc){
                if(err){
                    req.error = new send(200000);
                    return next();
                }else if(doc.code){
                    req.error = doc;
                    return next();
                }else{
                    var SEND = new send(0);
                    for(var i = 0; i < doc.docs.length; i++){
                        var a = parseInt(doc.docs[i].address.area[0]);
                        var b = parseInt(doc.docs[i].address.area[1]);
                        var c = parseInt(doc.docs[i].address.area[2]);
                        doc.docs[i].address.area = [
                            region[a].label,
                            region[a].children[b].label,
                            region[a].children[b].children[c].label
                        ];
                    };
                    core.api("open/qrcode", {msg: "personnel", type: "get"}, function(err, qrcode){
                        if(err){
                            req.error = new send(200000);
                            return next();
                        }else if(doc.code){
                            req.error = doc;
                            return next();
                        }else{
                            var size = parseInt(req.query.size) || 5;
                            var img = qr(qrcode.docs.url, {type: 'png', size: size});
                            SEND.docs = {
                                item : doc.docs,
                                token : token,
                                qrcode : img,
                                jssdk : wechat.getJSSDK(req.originalUrl)
                            };
                            console.log(SEND);
                            res.render("admin/merchant_list", SEND);
                        }
                    });
                }
            });
        }

    });
};

var POST = function(req, res, next){
    var body = req.body;
    var token = body.token;
    sign(token, function(doc){
        if(doc.code){
            res.send(doc);
        }else{
            core.api("merchant/list", {page: body.page, count: body.count}, function(err, doc){
                if(err){
                    var error = new send(200000);
                    return res.send(error);
                }else if(doc.code){
                    res.send(doc);
                }else{
                    var SEND = new send(0);
                    for(var i = 0; i < doc.docs.length; i++){
                        var a = parseInt(doc.docs[i].address.area[0]);
                        var b = parseInt(doc.docs[i].address.area[1]);
                        var c = parseInt(doc.docs[i].address.area[2]);
                        doc.docs[i].address.area = [
                            region[a].label,
                            region[a].children[b].label,
                            region[a].children[b].children[c].label
                        ];
                    };
                    SEND.docs = {
                        item : doc.docs
                    };
                    res.send(SEND);
                }
            });
        }

    });
};

exports.GET = GET;
exports.POST = POST;
